import { AutoFight } from "./AutoFight";
import { GTextFormat } from "../../../common/GTextFormat";
import { GCheckBox } from "../../../../com/mui/controls/GCheckBox";
import { Language } from "../../../../extend/language/Language";
import { ObjCreate } from "../common/ObjCreate";
import { GScrollPane } from "../../../../com/mui/controls/GScrollPane";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { Game } from "../../Game";
import { Cache } from "../../cache/Cache";
import { MonstersUtil } from "./MonstersUtil";
import { BossRule } from "../../rules/BossRule";
import { AutoFightRule } from "../../rules/AutoFightRule";
import { AutoFightInfo } from "./AutoFightInfo";
type int = number;
//class AttackPanel
    
    export  class AttackPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._autoFightInfo = AutoFight.localAutoFight;
            this._monsterArr = new Array<any>();
            this._monsterCbArr = [];
            this._curMonsterArr = [];
            this._colMonCbArr = [];
            this.init();
            return;
        }

        public getCurMonsterArr(): Array<any>
        {
            return this._curMonsterArr;
        }

        public get isTask(): Boolean
        {
            return this._isTask;
        }

        /* internal  */init(): void
        {
            var loc1=new GTextFormat("", 13, 16776960);
            this._collectMonsterTf = new GTextFormat("", 12, 48895);
            this._itemAttack = new GCheckBox();
            this._itemAttack.styleName = "CheckBoxStyle";
            this._itemAttack.label = Language.getString(34003);
            this._itemAttack.name = "autoAttackMonster";
            this._itemAttack.buttonMode = true;
            this._itemAttack.selected = true;
            this._itemAttack.width = 160;
            this._itemAttack.x = 20;
            this._itemAttack.y = 10;
            this._itemAttack.setStyle("textFormat", loc1);
            this._itemAttack.addEventListener(flash.events.Event.CHANGE, this.onItemAttackChangeHandler);
            this.addChild(this._itemAttack);
            this._collectMonster = new GCheckBox();
            this._collectMonster.styleName = "CheckBoxStyle";
            this._collectMonster.label = Language.getString(34012);
            this._collectMonster.name = "autoCollectMonster";
            this._collectMonster.buttonMode = true;
            this._collectMonster.selected = true;
            this._collectMonster.width = 120;
            this._collectMonster.x = 160;
            this._collectMonster.y = 10;
            this._collectMonster.setStyle("textFormat", this._collectMonsterTf);
            this._collectMonster.addEventListener(flash.events.Event.CHANGE, this.onCollectMonsterShowHandler);
            addChild(this._collectMonster);
            this._monsterSp = new Object /* flash.display.Sprite */();
            this._monsterSp.addEventListener(flash.events.Event.CHANGE, this.onMonsterCbChangeHandler);
            var loc2=ObjCreate.createGButton(Language.getString(34013), 425, 11, 50, 22, this);
            loc2.addEventListener(flash.events.MouseEvent.CLICK, this.onTaskBtnHandler);
            var loc3=ObjCreate.createGButton(Language.getString(34014), 480, 11, 50, 22, this);
            loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onSelectAllBtnHandler);
            var loc4;
            (loc4 = ObjCreate.createGButton(Language.getString(34015), 535, 11, 50, 22, this)).addEventListener(flash.events.MouseEvent.CLICK, this.onReverseBtnHandler);
            this._scrollPane = new GScrollPane();
            this._scrollPane.x = 17;
            this._scrollPane.y = 40;
            this._scrollPane.width = 575;
            this._scrollPane.height = 80;
            this._scrollPane.styleName = "GScrollPane";
            this._scrollPane.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._scrollPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane.source = this._monsterSp;
            this._scrollPane.scrollDrag = false;
            this._scrollPane.setScrollBarSize(19);
            addChild(this._scrollPane);
            this._scrollPane.update();
            return;
        }

        /* internal  */showMonster(): void
        {
            var loc4=null;
            var loc5=null;
            if (!Game.sceneInfo) 
            {
                return;
            }
            this._monsterArr = Cache.instance.scene.getFightRefreshPoint();
            this._monsterArr = MonstersUtil.getAllMonsters(this._monsterArr);
            while (this._monsterSp.numChildren > 0) 
            {
                this._monsterSp.removeChildAt(0);
            }
            var loc1=this.firstX;
            var loc2=this.firstY;
            var loc3=0;
            this._monsterCbArr.splice(0);
            this._colMonCbArr.splice(0);
            var loc6=0;
            var loc7=this._monsterArr;
            for(loc4 of loc7) 
            {
                if (BossRule.isCollectBoss(loc4.type)) 
                {
                    if (!this._autoFightInfo.autoCollect) 
                    {
                        continue;
                    }
                }
                (loc5 = new GCheckBox()).label = loc4.name;
                loc5.width = 140;
                loc5.x = loc1;
                loc5.y = loc2;
                loc5.buttonMode = true;
                loc5.selected = true;
                if (BossRule.isCollectBoss(loc4.type)) 
                {
                    loc5.styleName = "CheckBoxStyle";
                    loc5.setStyle("textFormat", this._collectMonsterTf);
                }
                else 
                {
                    loc5.styleName = "GCheckBox";
                }
                this._monsterSp.addChild(loc5);
                this._monsterCbArr.push(loc5);
                if (BossRule.isCollectBoss(loc4.type)) 
                {
                    this._colMonCbArr.push(loc5);
                }
                ++loc3;
                loc1 = loc1 + loc5.width;
                if (loc3 % 4 != 0) 
                {
                    continue;
                }
                loc1 = this.firstX;
                loc2 = loc2 + loc5.height + 5;
            }
            this._scrollPane.update();
            if (this._isTask && !AutoFightRule.isAutoSelectedBoss()) 
            {
                this.getTaskMonster();
            }
            if (this._itemAttack.selected) 
            {
                this.getAttackMonster();
            }
            return;
        }

        /* internal  */getAttackMonster(): void
        {
            var loc2=null;
            var loc4=null;
            var loc1=this._monsterCbArr.length;
            this._curMonsterArr.length = 0;
            var loc3=0;
            while (loc3 < loc1) 
            {
                loc2 = this._monsterCbArr[loc3] as GCheckBox;
                if (loc2.selected) 
                {
                    var loc5=0;
                    var loc6=this._monsterArr;
                    for(loc4 of loc6) 
                    {
                        if (loc2.label != loc4.name) 
                        {
                            continue;
                        }
                        this._curMonsterArr[this._curMonsterArr.length] = loc4.code;
                        break;
                    }
                }
                ++loc3;
            }
            if (this._autoFightInfo) 
            {
                this._autoFightInfo.monsterArr = this._curMonsterArr;
            }
            this._autoFightInfo.alter = true;
            return;
        }

        /* internal  */onItemAttackChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this._autoFightInfo.setAutoFightInfo(this._itemAttack.name, this._itemAttack.selected);
            this._autoFightInfo.alter = true;
            return;
        }

        /* internal  */onCollectMonsterShowHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=null;
            this._autoFightInfo.setAutoFightInfo(this._collectMonster.name, this._collectMonster.selected);
            var loc2=0;
            while (loc2 < this._colMonCbArr.length) 
            {
                loc1 = this._colMonCbArr[loc2] as GCheckBox;
                if (this._collectMonster.selected) 
                {
                    loc1.visible = true;
                }
                else 
                {
                    loc1.visible = false;
                }
                ++loc2;
            }
            if (this._itemAttack.selected) 
            {
                this.getAttackMonster();
            }
            return;
        }

        /* internal  */onTaskBtnHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.getTaskMonster();
            this._autoFightInfo.alter = true;
            this._isTask = true;
            return;
        }

        /* internal  */getTaskMonster(arg1: Boolean=true): void
        {
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc8=null;
            var loc9=null;
            var loc1=this._monsterCbArr.length;
            var loc2=0;
            while (loc2 < loc1) 
            {
                this._monsterCbArr[loc2].selected = false;
                ++loc2;
            }
            var loc3=Cache.instance.task.getTaskMonster(true).concat();
            var loc4=[];
            this._curMonsterArr.length = 0;
            loc6 = 0;
            while (loc6 < loc3.length) 
            {
                loc5 = loc3[loc6];
                loc4.push(loc5.id);
                ++loc6;
            }
            if (!arg1) 
            {
                loc9 = AutoFight.currentAutoFight.monsterArr.concat();
                loc6 = 0;
                while (loc6 < loc9.length) 
                {
                    loc7 = 0;
                    while (loc7 < loc4.length) 
                    {
                        if (loc9[loc6] == loc4[loc7]) 
                        {
                            loc9.splice(loc6, 1);
                        }
                        ++loc7;
                    }
                    ++loc6;
                }
                loc6 = 0;
                while (loc6 < loc9.length) 
                {
                    loc4.push(loc9[loc6]);
                    ++loc6;
                }
            }
            loc6 = 0;
            while (loc6 < this._monsterArr.length) 
            {
                loc8 = this._monsterArr[loc6];
                loc7 = 0;
                while (loc7 < loc4.length) 
                {
                    if (loc8.code == loc4[loc7]) 
                    {
                        (this._monsterCbArr[loc6] as GCheckBox).selected = true;
                    }
                    ++loc7;
                }
                ++loc6;
            }
            this._curMonsterArr = loc4;
            AutoFight.localAutoFight.monsterArr = loc4;
            return;
        }

        /* internal  */onSelectAllBtnHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc3=null;
            var loc1=this._monsterCbArr.length;
            var loc2=0;
            while (loc2 < loc1) 
            {
                if ((loc3 = this._monsterCbArr[loc2] as GCheckBox).selected == false) 
                {
                    loc3.selected = true;
                }
                ++loc2;
            }
            this.getAttackMonster();
            this._isTask = false;
            return;
        }

        /* internal  */onReverseBtnHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc3=null;
            var loc1=this._monsterCbArr.length;
            var loc2=0;
            while (loc2 < loc1) 
            {
                if ((loc3 = this._monsterCbArr[loc2] as GCheckBox).selected != false) 
                {
                    if (loc3.selected == true) 
                    {
                        loc3.selected = false;
                    }
                }
                else 
                {
                    loc3.selected = true;
                }
                ++loc2;
            }
            this.getAttackMonster();
            this._isTask = false;
            return;
        }

        /* internal  */onMonsterCbChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            if (typeof arg1.target === "gcheckbox") 
            {
                this.getAttackMonster();
            }
            return;
        }

        /* internal  */resetPanel(arg1: Boolean, arg2: Array<any>): void
        {
            this._itemAttack.selected = arg1;
            return;
        }

        public update(arg1: Boolean=false): void
        {
            if (arg1) 
            {
                this.showMonster();
            }
            if (!AutoFightRule.isAutoSelectedBoss()) 
            {
                this.getTaskMonster(false);
            }
            this.resetPanel(this._autoFightInfo.attackMonster, this._autoFightInfo.monsterArr);
            return;
        }

        /* internal */ /* const */firstX: int=0;

        /* internal */ /* const */firstY: int=0;

        /* internal */ /* var */_autoFightInfo: AutoFightInfo;

        /* internal */ /* var */_monsterArr: Array<any>;

        /* internal */ /* var */_monsterSp: Object /* flash.display.Sprite */;

        /* internal */ /* var */_monsterCbArr: Array<any>;

        /* internal */ /* var */_itemAttack: GCheckBox;

        /* internal */ /* var */_collectMonster: GCheckBox;

        /* internal */ /* var */_curMonsterArr: Array<any>;

        /* internal */ /* var */_scrollPane: GScrollPane;

        /* internal */ /* var */_isTask: Boolean=true;

        /* internal */ /* var */_collectMonsterTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_colMonCbArr: Array<any>;
    }
